<?php

namespace App\Http\Controllers\Purchase;

use App\Http\Controllers\BaseController;
use App\Http\Validate\Purchase\PurchaseSettlePriceLog as PurchasePurchaseSettlePriceLog;
use App\Models\Purchase\PurchaseSettlePriceLog;
use App\Models\Steel\SteelAttrList;
use Illuminate\Http\Request;


/**
 * 后结算取价
 */
class PurchaseSettlePriceLogController extends BaseController
{
    /**
     * 后结算取价-列表
     */
    public function getlist(Request $request)
    {

        $cpid = $this->mangerinfo['cpid'];
        $list = PurchaseSettlePriceLog::getList($request, $cpid);
        return response()->json(['code' => '000', 'msg' => '操作成功', 'data' => $list]);
    }

    public function dopostadd(Request $request)
    {
        $cpid = $this->mangerinfo['cpid'];
        $req_data = $request->only(['type', 'base_price', 'steel_factory_id','create_at']);
        $Validate = new PurchasePurchaseSettlePriceLog();
        if (!$Validate->check($req_data)) {
            return response()->json(['code' => '001', 'msg' => $Validate->getError()]);
        }

        $type = $req_data['type'] ?? 1;

        if ($type == 1) {
            $steel_factoryinfo = SteelAttrList::where([['id', $req_data['steel_factory_id']], ['cpid', $cpid], ['status', 1], ['type_id', 3]])->first();
            if (!$steel_factoryinfo) {
                return response()->json(['code' => '001', 'msg' => '无效钢厂']);
            }

            $data_steel = [
                'steel_factory_id' => $req_data['steel_factory_id'] ?? 0,
                'steel_factory_name' => $steel_factoryinfo->attr_val_name,
            ];
        } else {
            $data_steel = [];
        }
        if ($type == 1) {
            $iscurrentdayadd=PurchaseSettlePriceLog::where([
                ['cpid', $cpid] ,['type', $type],
                ['is_del',1],['status',1],
                ['steel_factory_id', $req_data['steel_factory_id']],
                ['create_at','>=',strtotime($req_data['create_at'])],
                ['create_at','<=',strtotime($req_data['create_at'])+86399]
            ])->first();
            if($iscurrentdayadd){
                return response()->json(['code' => '001', 'msg' => '今日已添加']);
            }
        }else{
            $iscurrentdayadd=PurchaseSettlePriceLog::where([['cpid', $cpid] ,['type', $type],['is_del',1],['status',1],['create_at','>=',strtotime(date("Y-m-d")." 00:00:00")],['create_at','<=',strtotime(date("Y-m-d")." 23:59:59")] ])->first();
            if($iscurrentdayadd){
                return response()->json(['code' => '001', 'msg' => '今日已添加']);
            }
        }


        $data = [
            'cpid' => $cpid,
            'type' => $type,
            'base_price' => $req_data['base_price'],
            'create_at' => strtotime($req_data['create_at']),
            'update_at' => time()
        ] + $data_steel;
        PurchaseSettlePriceLog::create($data);
        return response()->json(['code' => '000', 'msg' => '新增成功']);
    }

    public function getinfo(Request $request)
    {
        $cpid = $this->mangerinfo['cpid'];
        $log_id = $request->log_id ?? 0;
        $info = PurchaseSettlePriceLog::where([['id', $log_id], ['cpid', $cpid], ['status', 1], ['is_del', 1]])->first();
        if (!$info) {
            return response()->json(['code' => '001', 'msg' => '参数错误']);
        }
        return response()->json(['code' => '000', 'msg' => '编辑成功', 'data' => ['info' => $info]]);
    }

    public function dopostedit(Request $request)
    {
        $cpid = $this->mangerinfo['cpid'];
        $req_data = $request->only(['base_price', 'log_id','create_at']);
        $Validate = new PurchasePurchaseSettlePriceLog('edit');
        if (!$Validate->check($req_data)) {
            return response()->json(['code' => '001', 'msg' => $Validate->getError()]);
        }
        $loginfo = PurchaseSettlePriceLog::where([['id', $req_data['log_id']], ['cpid', $cpid], ['status', 1], ['is_del', 1]])->first();
        if (!$loginfo) {

            return response()->json(['code' => '001', 'msg' => '无效参数']);
        }

        $data = [
            'base_price' => $req_data['base_price'],
            'update_at' => time()
        ];
//        if ($loginfo->type == 1) {
//            $iscurrentdayadd=PurchaseSettlePriceLog::where([
//                ['cpid', $cpid] ,
//                ['type', 1],
//                ['is_del',1],
//                ['status',1],
//                ['steel_factory_id', $loginfo->steel_factory_id],
//                ['create_at','>=',strtotime($req_data['create_at'])],
//                ['create_at','<=',strtotime($req_data['create_at'])+86399],
//                ['id', '!=', $req_data['log_id']]
//            ])->exists();
//            if($iscurrentdayadd){
//                return response()->json(['code' => '001', 'msg' => '今日已添加']);
//            }
//            $data['create_at'] = strtotime($req_data['create_at']);
//        }
        PurchaseSettlePriceLog::where([['id', $req_data['log_id']], ['cpid', $cpid], ['status', 1], ['is_del', 1]])->update($data);
        return response()->json(['code' => '000', 'msg' => '编辑成功']);
    }

    /**
     * 冻结
     */
    public function changestatus(Request $request)
    {
        $cpid = $this->mangerinfo['cpid'];
        $log_id = $request->log_id ?? 0;
        $info = PurchaseSettlePriceLog::where([['id', $log_id], ['cpid', $cpid], ['status', 1], ['is_del', 1]])->first();
        if (!$info) {
            return response()->json(['code' => '001', 'msg' => '参数错误']);
        }
        $status = 2;
        PurchaseSettlePriceLog::where([['id', $log_id]])->update(['status' => $status, 'update_at' => time()]);
        return response()->json(['code' => '000', 'msg' => '操作成功']);
    }
    /**
     * 删除
     */
    public function dodel(Request $request)
    {
        $cpid = $this->mangerinfo['cpid'];
        $log_id = $request->log_id ?? 0;
        $info = PurchaseSettlePriceLog::where([['id', $log_id], ['cpid', $cpid], ['status', 1], ['is_del', 1]])->first();
        if (!$info) {
            return response()->json(['code' => '001', 'msg' => '参数错误']);
        }
        PurchaseSettlePriceLog::where([['id', $log_id]])->update(['is_del' => 2, 'del_at' => time()]);
        return response()->json(['code' => '000', 'msg' => '操作成功']);
    }
}
